﻿package com.badibadi.activity;

import java.util.HashMap;
import java.util.Map;

import cn.sharesdk.facebook.Facebook;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.sina.weibo.SinaWeibo.ShareParams;
import cn.sharesdk.system.text.ShortMessage;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;

import com.badibadi.adapter.ClubMyClubListAdapter;
import com.badibadi.infos.Results;
import com.badibadi.mytools.CommonUtils;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.PhoneActivity;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.R.layout;
import com.badibadi.uniclubber.R.menu;
//import com.lidroid.xutils.util.LogUtils;
import com.wyy.twodimcode.CaptureActivity;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

//tiaoguo share_qzone
public class CreationNewClub4Activity extends BaseActivity implements // creation_new_club_saoyisao
		OnClickListener {

	private String TAG = "CreationNewClub4Activity";
	private Button tiaoguo;
	private Intent intent = null;

	private LinearLayout btn_saoyisao;
	private String erweimasourse;

	private String clubid = "";

	private String uid = "";
	private String clubname = "";
	private Results results;

	// 1个站内邀请的按钮
	private LinearLayout share_zanleiyaoqing;// share_qq
	// 四个站外分享的按钮
	private LinearLayout share_qq, share_sina, share_qzone, share_facebook;
	private ShareParams sp;
	Platform share;
	String User = "";
	String PhoneNumber = "";
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(CreationNewClub4Activity.this);
				Utils.showMessage(CreationNewClub4Activity.this, getResources()
						.getString(R.string.l_net_error));
				break;
			case 2:
				Utils.ExitPrgress(CreationNewClub4Activity.this);
				Utils.showMessage(CreationNewClub4Activity.this, getResources()
						.getString(R.string.l_xb67));
				break;
			case 3:
				Utils.ExitPrgress(CreationNewClub4Activity.this);
				Utils.showMessage(CreationNewClub4Activity.this, getResources()
						.getString(R.string.l_xa10));
				break;
			case 5:
				Utils.ExitPrgress(CreationNewClub4Activity.this);
				try {
//					LogUtils.d("返回的路径是："
//							+ msg.getData().getString("msg",
//									"http://www.uniclubber.com"));
					int num = msg.getData().getInt("num", 0);
					if (num != 1 && num != 3 && num != 4) {
						sp.setTitleUrl(msg.getData().getString("msg",
								"http://www.uniclubber.com"));
					}
					// 执行图文分享
					share.share(sp);
				} catch (Exception e) {
					// TODO: handle exception
				}
				break;
			case 6://
				Utils.ExitPrgress(CreationNewClub4Activity.this);
				try {
					Utils.showMessage(CreationNewClub4Activity.this,
							getResources().getString(R.string.l_xb113));
				} catch (Exception e) {
					// TODO: handle exception
				}
				break;

			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		ShareSDK.initSDK(CreationNewClub4Activity.this);
		setContentView(R.layout.creation_new_club3);
		this.findViewById(R.id.return_btn).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						finish();
					}
				});
		clubid = getIntent().getStringExtra("clubid");
		uid = getIntent().getStringExtra("uid");
		clubname = getIntent().getStringExtra("clubname");
		init();

	}

	private void init() {
		// TODO Auto-generated method stub

		share_qq = (LinearLayout) findViewById(R.id.share_qq);
		share_qq.setOnClickListener(this);
		share_sina = (LinearLayout) findViewById(R.id.share_sina);
		share_sina.setOnClickListener(this);
		share_qzone = (LinearLayout) findViewById(R.id.share_qzone);
		share_qzone.setOnClickListener(this);
		share_facebook = (LinearLayout) findViewById(R.id.share_facebook);
		share_facebook.setOnClickListener(this);

		tiaoguo = (Button) findViewById(R.id.tiaoguo);
		btn_saoyisao = (LinearLayout) findViewById(R.id.creation_new_club_saoyisao);
		btn_saoyisao.setOnClickListener(CreationNewClub4Activity.this);
		tiaoguo.setOnClickListener(CreationNewClub4Activity.this);
		share_zanleiyaoqing = (LinearLayout) findViewById(R.id.share_zanleiyaoqing);
		share_zanleiyaoqing.setOnClickListener(CreationNewClub4Activity.this);
		this.findViewById(R.id.weixin).setOnClickListener(this);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		switch (requestCode) {
		case 1:
			if (requestCode == 1 && resultCode == PhoneActivity.phone) {
				if (data != null) {
					User = data.getStringExtra("User");
					// data.getStringExtra("PhoneNumber");

					PhoneNumber = data.getStringExtra("PhoneNumber");
					share(4);
				}
			}
			// if (data != null) {
			// String result = data.getStringExtra("result");
			//    
			// if (result != null)
			// erweimasourse = result;
			// EnterWebView(erweimasourse);
			//
			// }
			break;

		default:
			break;
		}

		if (requestCode == 21 && resultCode == 21) {
			String res = data.getStringExtra("res");
			innerInvite(res);
		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	// private void EnterWebView(String sourse) {
	// // TODO Auto-generated method stu
	//    
	// if (sourse.indexOf("http://") == -1) {
	// sourse = "http://" + sourse;
	//    
	// }
	// Uri uri = Uri.parse(sourse);
	// intent = new Intent(Intent.ACTION_VIEW, uri);
	// if (intent != null) {
	// startActivity(intent);
	// intent = null;
	// }
	//
	// }

	private void innerInvite(final String invite_user) {
		Utils.showPrgress(CreationNewClub4Activity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {

			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("cid", clubid);
				map.put("invite_user", invite_user);
				map.put("uid", Utils.getUid(CreationNewClub4Activity.this));
//				   ("站内邀请提交数据是:" + map.toString());
				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "club/innerInvite");
				if (content != null) {
					results = Utils.checkResult_NNN(getApplicationContext(),
							content);
					if (results != null && !results.getRetmsg().equals("null")
							&& results.isRet()) {
						handler.sendEmptyMessage(2);
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tiaoguo:
			intent = new Intent(CreationNewClub4Activity.this,
					ClubMyClubActivity.class);

			break;
		case R.id.share_zanleiyaoqing:// 站内邀请
			Intent intent = new Intent(CreationNewClub4Activity.this,
					ChoiceCrewActivity.class);
			startActivityForResult(intent, 21);
			break;
		case R.id.share_qq:
			share(0);
			break;
		case R.id.share_sina:
			share(1);
			break;
		case R.id.share_qzone:
			// Utils.showMessage(CreationNewClub4Activity.this, "响应了");
			share(2);
			break;
		case R.id.share_facebook:
			share(3);
			break;
		case R.id.weixin:
			// Utils.showMessage(CreationNewClub4Activity.this, "响应了wer");
			share(5);
			break;
		case R.id.creation_new_club_saoyisao:
			// intent=new Intent
			// Intent it = new Intent(CreationNewClub4Activity.this,
			// PhoneActivity.class);
			// startActivityForResult(it, 1);
			//
			// intent = null;
			share(4);
			break;
		}
		if (intent != null) {
			startActivity(intent);
			overridePendingTransition(R.anim.slide_left, R.anim.slide_right);
			intent = null;
		}
	}

	/**
	 * 分享
	 * 
	 * @param 0:qq,1:sina,2,twitter,3.facebook;
	 */
	public void share(int num) {
		sp = new ShareParams();

		String aa = String.format(getResources().getString(R.string.wx2_16),
				clubname);

		share = null;
		switch (num) {
		case 0:// QQ
			sp.setText(aa);
			sp.setTitle(getResources().getString(R.string.share));
			sp.setTitleUrl(Constants.share_no_image_share_address);
			sp.setImageUrl(Constants.share_no_image_share_address);
			share = ShareSDK
					.getPlatform(CreationNewClub4Activity.this, QQ.NAME);
			break;
		case 1:// sinaweibo
			sp.setText(aa);

			sp.setImageUrl(Constants.share_no_image_share_address);
			share = ShareSDK.getPlatform(CreationNewClub4Activity.this,
					SinaWeibo.NAME);
			break;
		case 2:// QZone title titleUrl text site siteUrl
			sp.setText(aa);
			sp.setTitle(getResources().getString(R.string.share));
			sp.setTitleUrl(Constants.share_no_image_share_address);
			sp.setSite(getResources().getString(R.string.wx2_16));
			sp.setSiteUrl(Constants.share_no_image_share_address);
			sp.setImageUrl(Constants.share_no_image_share_address);
			share = ShareSDK.getPlatform(CreationNewClub4Activity.this,
					QZone.NAME);
//			LogUtils.d("运行到了这里325");
			break;
		case 3:// Facebook
			sp.setText(aa);
			share = ShareSDK.getPlatform(CreationNewClub4Activity.this,
					Facebook.NAME);
			break;
		case 4:// ShortMessage
			sp.setAddress("");
			sp.setText(aa);
			sp.setTitle(getResources().getString(R.string.share));
			// sp.setImageUrl(Constants.share_no_image_share_address);

			share = ShareSDK.getPlatform(CreationNewClub4Activity.this,
					ShortMessage.NAME);
			break;
		case 5:// Wechat
			sp.setText(aa);
			sp.setTitle(getResources().getString(R.string.share));
			sp.setImageUrl(Constants.share_no_image_share_address);
			sp.setAddress("" + PhoneNumber);
			// qq空间的必须要的两个
			sp.setSite(getResources().getString(R.string.wx2_16));
			sp.setImageUrl(Constants.share_no_image_share_address);
			sp.setShareType(Wechat.SHARE_TEXT);
			// 这才是分享过去的链接，用户点击他后进入链接
			sp.setTitleUrl(getResources().getString(R.string.wx2_15));
			share = ShareSDK.getPlatform(CreationNewClub4Activity.this,
					Wechat.NAME);
			break;
		}

		share.setPlatformActionListener(new PlatformActionListener() {

			@Override
			public void onError(Platform platform, int arg1, Throwable arg2) {
				// TODO Auto-generated method stub
//				LogUtils.d("qzone350");
				handler.sendEmptyMessage(6);
			}

			@Override
			public void onComplete(Platform platform, int arg1,
					HashMap<String, Object> arg2) {
				// TODO Auto-generated method stub
//				   
//						+ "arg2=" + arg2);
				handler.sendEmptyMessage(2);
//				LogUtils.d("qzone329");
			}

			@Override
			public void onCancel(Platform platform, int arg1) {
				// TODO Auto-generated method stub
//				LogUtils.d("qzone365");
			}
		}); // 设置分享事件回调
		// 执行图文分享
		// share.share(sp);
		share.SSOSetting(false);
		share.showUser(null);
		outerShareUrl(CreationNewClub4Activity.this, "club",
				Utils.getUid(CreationNewClub4Activity.this), clubid, clubid,
				num);
	}

	/**
	 * 站外分享返回相应链接----POST
	 * 
	 * @param $type
	 *            分享类型 photo_img：相片 humor_trends:心情动态 record_article：记录（记）
	 *            club:创建的俱乐部 activity：发起的活动 friend：朋友
	 * @param $uid
	 *            被分享的内容所属的用户
	 * @param $cid
	 *            被分享的内容所属的俱乐部 个人内容时$cid=0
	 * @param $conId
	 *            被分享的内容ID
	 * */
	public void outerShareUrl(Context ct, final String type, final String uid,
			final String cid, final String conId, final int num) {
		// Map<String, Object> map = new HashMap<String, Object>();
		// map.put("uid", uid);
		// map.put("type", type);
		// map.put("cid", cid);
		// map.put("conId", conId);
		// CommonUtils.CommomPostfunction(ct, Constants.BadiUrl_1 + "/"
		// + "web/outerShareUrl", map, handler, 0, 0, 5, 6);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", uid);
				map.put("type", type);
				map.put("cid", cid);
				map.put("conId", conId);
				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "web/outerShareUrl");
				//    
				if (content != null) {
					Results results = Utils.checkResult_NNN(
							CreationNewClub4Activity.this, content);
					if (results != null && !results.getRetmsg().equals("null")) {
						//    
						try {
							// list = JSONUtils.getListByJsonString(
							// results.getRetmsg(),
							// Pre_message_total.class);

							if (results.isRet() == true) {
								Message msg = new Message();
								msg.what = 5;
								Bundle bun = new Bundle();
								bun.putString("msg", results.getRetmsg());
								bun.putInt("num", num);
								msg.setData(bun);

								handler.sendMessage(msg);
							} else {
								Message msg = new Message();
								msg.what = 5;
								Bundle bun = new Bundle();
								bun.putString("msg", results.getRetmsg());
								bun.putInt("num", num);
								msg.setData(bun);
								handler.sendMessage(msg);
							}
						} catch (Exception e) {
							// TODO Auto-generated catch block
							   
							handler.sendEmptyMessage(6);
						}
					} else {
						handler.sendEmptyMessage(0);
					}
				} else {
					handler.sendEmptyMessage(0);
				}

			}
		});
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		ShareSDK.stopSDK(this);
		super.onDestroy();

	}

}
